package com.ikangtai.shecare.common.al;

import com.ikangtai.shecare.common.util.AppLog;
import com.ikangtai.shecare.common.util.DateUtil;

import java.util.ArrayList;

public class ALDayOutputList {
	public static ArrayList<DayUnitDSOutput> getALDayOutputList(int type, CycleUnitDS[] cycleInput,CycleUnitDS[] cycleOutputBefore,UserUnitDS userdata){

		CycleUnitDS[] cycleOutput = new CycleUnitDS[17];
		int cycleOutputLength = 0;
		for(int i = 0; i < 17; i++) {

			if (cycleOutputBefore[i].menstruationStartForecast > 100000 || cycleOutputBefore[i].menstruationStartConfirm > 100000) {
				//TODO:cycleOutputBefore拷贝到cycleoutput

				cycleOutput[cycleOutputLength] = cycleOutputBefore[i];

				AppLog.i("alDayOutputList, i1 = " + i + ", cycleOutput.cycleNumber = " + cycleOutput[cycleOutputLength].cycleNumber +
						", menstruationStartConfirm =" + DateUtil.getDateTimeStr(cycleOutput[cycleOutputLength].menstruationStartConfirm) +
						", menstruationStartForecast = " + DateUtil.getDateTimeStr(cycleOutput[cycleOutputLength].menstruationStartForecast));

				cycleOutputLength ++;
			}

		}

		ArrayList<DayUnitDSOutput> daysData = new ArrayList<DayUnitDSOutput>();

		int cycleInputLength =  cycleInput.length;
		if(cycleOutputLength <= 0) return daysData;
		AppLog.i("alDayOutputList, cycleinputlength = "+cycleInputLength + "  cycleOutputlength = "+cycleOutputLength);
/*

		if(type == 102){

			
			long ps = cycleOutput[0].menstruationStartConfirm > 100?cycleOutput[0].menstruationStartConfirm:
				cycleOutput[0].menstruationEndForecast;
			daysData.addAll(outputExtraDayBeforeAll(ps, delayCount, 0));
		}else if(type == 4){
			long cyOutPS0 = cycleOutput[0].menstruationStartConfirm > 100? cycleOutput[0].menstruationStartConfirm:cycleOutput[0].menstruationStartForecast;
			long cyInPS0  = cycleInput[0].menstruationStartConfirm > 100?cycleInput[0].menstruationStartConfirm:cycleInput[0].menstruationStartForecast;
			CycleUnitDS cycleInput0 = cycleInput[0];
			CycleUnitDS cycleOutput0 = cycleOutput[0];

			if(cyOutPS0 > cyInPS0+100){

				long start = cycleInput0.menstruationStartConfirm > 100 ? cycleInput0.menstruationStartConfirm:
					cycleInput0.menstruationStartForecast;
				long end = cycleOutput0.menstruationStartConfirm > 100 ? cycleOutput0.menstruationStartConfirm:
					cycleOutput0.menstruationStartForecast;
				return deleteDaysOutput(start, end, 0, delayCount);
			}
		}else if(type == 2){
			CycleUnitDS cycleOutput0 = cycleOutput[0];
			CycleUnitDS cycleInput0 = cycleInput[0];
			if(cycleOutput0.cycleNumber < cycleInput0.cycleNumber){

				long ps = cycleOutput0.menstruationStartConfirm > 100?cycleOutput0.menstruationStartConfirm:
					cycleOutput0.menstruationEndForecast;
				daysData.addAll(outputExtraDayBeforeAll(ps, delayCount, 0));
			}
			
		}else{
			delayCount = 0;
		}
		*/
		int x= 0;
		int result = 0;
		//算法需要区分：
		//1,对于过去周期的各种修改，
		// 1.1 如果插入的是第一个周期，则仅仅修改本本周，cycleOutput仅仅就一个周期，
		// 1.2 如果插入的是中间一个周期会牵涉到前后两个周期，
		// 1.3 对经期结束的修改仅仅牵涉到当前周期
		// 1.4 删除第一个周期，仅仅把第一个周期牵涉到的那些天数进行设置成-99
		// 1.5 删除非第一个周期，把中间的两个周期的天数进行合并
	
		//2，对于当前周期的修改，
		// 2.1 插入一个新的经期开始，会影响前后两个周期的以及后面预测的3个周期。
		// 2.2 修改经期开始，会影响前后两个周期以及之后预测的3个周期
			// 2.3 插入经期结束，会影响本周起以及后面预测的3个周期
		// 2.4 删除本周期的经期开始，cycle会输出上个周期和根据上个周期预测的3个cycle
		
		//3，对于过去周期中信息的输入,仅仅会影响此周期
		//4,对于当前周期中信息的输入,会影响当前周期以及之后的周期
		if(type == 1){
			AppLog.i("alDayOutputList, vvvv   1");

			long ps = cycleOutput[0].menstruationStartConfirm;
			daysData.addAll(outputExtraDayBeforeAll(ps,10,0));

			for(x = 0; x < cycleOutputLength-1; x++){
				CycleUnitDS cycleOutputX = cycleOutput[x];
				CycleUnitDS cycleOutputNext = cycleOutput[x + 1];
				if (cycleOutputNext.menstruationStartConfirm < 100) {
					daysData.addAll(cycleOutGetDayOut(cycleOutputX, 0, cycleOutputNext));
				} else {
					daysData.addAll(cycleOutGetDayOut(cycleOutputX, 1, cycleOutputNext));
				}
			}
			daysData.addAll(cycleOutGetDayOut(cycleOutput[cycleOutputLength-1], 0, cycleOutput[cycleOutputLength-1]));
		}else if(type == 102){
			AppLog.i("alDayOutputList, vvvv   102");

			long ps = cycleOutput[0].menstruationStartConfirm;
			daysData.addAll(outputExtraDayBeforeAll(ps,10,0));

			for(x = 0; x < cycleOutputLength-1; x++){
				CycleUnitDS cycleOutputX = cycleOutput[x];
				CycleUnitDS cycleOutputNext = cycleOutput[x + 1];
				if (cycleOutputNext.menstruationStartConfirm < 100) {
					daysData.addAll(cycleOutGetDayOut(cycleOutputX, 0, cycleOutputNext));
				} else {
					daysData.addAll(cycleOutGetDayOut(cycleOutputX, 1, cycleOutputNext));
				}
			}
			daysData.addAll(cycleOutGetDayOut(cycleOutput[cycleOutputLength-1], 0, cycleOutput[cycleOutputLength-1]));
		}else if(type == 2 || type == 6){
			if(cycleInput[0].cycleNumber == cycleOutput[0].cycleNumber+1){
				//对过去经期数据的插入
				AppLog.i("alDayOutputList, vvvv   2");
				if(cycleOutput[0].menstruationStartConfirm < cycleInput[0].menstruationStartConfirm){
					//插入的是第一个周期
					long ps = cycleOutput[0].menstruationStartConfirm;
					daysData.addAll(outputExtraDayBeforeAll(ps,10,0));
					daysData.addAll(cycleOutGetDayOut(cycleOutput[0], 1, cycleInput[0]));
				}else{
					//插入的是中间的周期
					for(x = 0; x < cycleOutputLength-1; x++){
						CycleUnitDS cycleOutputX = cycleOutput[x];
						CycleUnitDS cycleOutputNext = cycleOutput[x + 1];
						if (cycleOutputNext.menstruationStartConfirm < 100) {
							daysData.addAll(cycleOutGetDayOut(cycleOutputX, 0, cycleOutputNext));
						} else {
							daysData.addAll(cycleOutGetDayOut(cycleOutputX, 1, cycleOutputNext));
						}
					}
					int num = cycleOutput[x].cycleNumber+1;
					int y = 0;
					for(y = 0; y < cycleInputLength; y++){
						if(cycleInput[y].cycleNumber == num) break;
					}
					if( y == cycleInputLength){
						daysData.addAll(cycleOutGetDayOut(cycleOutput[x], 0, cycleOutput[x]));
					}else{
						CycleUnitDS cycleOutputNext = cycleInput[y];
						if (cycleOutputNext.menstruationStartConfirm < 100) {
							daysData.addAll(cycleOutGetDayOut(cycleOutput[x], 0, cycleOutputNext));
						} else {
							daysData.addAll(cycleOutGetDayOut(cycleOutput[x], 1, cycleOutputNext));
						}
					}
					
					
				}
				AppLog.i("alDayOutputList, vvvv   2.1");
			}else if(cycleInput[cycleInputLength-1].cycleNumber == cycleOutput[0].cycleNumber){
				//新插入的一个当前周期
				for(x = 0; x < cycleOutputLength-1; x++){
					CycleUnitDS cycleOutputX = cycleOutput[x];
					CycleUnitDS cycleOutputNext = cycleOutput[x + 1];
					if (cycleOutputNext.menstruationStartConfirm < 100) {
						daysData.addAll(cycleOutGetDayOut(cycleOutputX, 0, cycleOutputNext));
					} else {
						daysData.addAll(cycleOutGetDayOut(cycleOutputX, 1, cycleOutputNext));
					}
				}
			}else if(cycleInput[cycleInputLength-1].cycleNumber == cycleOutput[0].cycleNumber + 1){
				//对当前的周期进行修改
				AppLog.i("alDayOutputList, vvvv   3");
				for(x = 0; x < cycleOutputLength-1; x++){
					CycleUnitDS cycleOutputX = cycleOutput[x];
					CycleUnitDS cycleOutputNext = cycleOutput[x + 1];
					if (cycleOutputNext.menstruationStartConfirm < 100) {
						daysData.addAll(cycleOutGetDayOut(cycleOutputX, 0, cycleOutputNext));
					} else {
						daysData.addAll(cycleOutGetDayOut(cycleOutputX, 1, cycleOutputNext));
					}
				}
				AppLog.i("alDayOutputList, vvvv   3.1");
			}else if(cycleInput[0].cycleNumber == cycleOutput[0].cycleNumber && cycleOutputLength == 1){
				//对历史的第一个经期开始进行修改
				AppLog.i("alDayOutputList, vvvv   4");
				long ps = cycleOutput[0].menstruationStartConfirm;
				daysData.addAll(outputExtraDayBeforeAll(ps,10,0));
				daysData.addAll(cycleOutGetDayOut(cycleOutput[0], 1, cycleInput[1]));
				AppLog.i("alDayOutputList, vvvv   4.1");
			}else if(cycleOutputLength == 2 && cycleInput[0].cycleNumber < cycleOutput[1].cycleNumber &&
					cycleInput[cycleInputLength-1].cycleNumber > cycleOutput[1].cycleNumber){
				//对历史中经期进行修改
				AppLog.i("alDayOutputList, vvvv   5");
				int num = cycleOutput[cycleOutputLength-1].cycleNumber+1;
				for(x = 0; x < cycleOutputLength-1; x++){
					CycleUnitDS cycleOutputX = cycleOutput[x];
					CycleUnitDS cycleOutputNext = cycleOutput[x + 1];
					if (cycleOutputNext.menstruationStartConfirm < 100) {
						daysData.addAll(cycleOutGetDayOut(cycleOutputX, 0, cycleOutputNext));
					} else {
						daysData.addAll(cycleOutGetDayOut(cycleOutputX, 1, cycleOutputNext));
					}
				}
				int y;
				for( y = 0; y < cycleInputLength; y++){
					if(cycleInput[y].cycleNumber == num) break;
				}
				if(y == cycleInputLength){
					daysData.addAll(cycleOutGetDayOut(cycleOutput[x], 0, cycleOutput[x]));
				}else{
					daysData.addAll(cycleOutGetDayOut(cycleOutput[x], 1, cycleInput[y]));
				}
				AppLog.i("alDayOutputList, vvvv   5.1");
				
			}
		}else if(type == 3){
			//插入经期结束
			if(cycleInput[cycleInputLength-1].cycleNumber == cycleOutput[0].cycleNumber){
				//插入的是当前周期的经期结束
				AppLog.i("alDayOutputList, vvvv   6");
				for(x = 0; x < cycleOutputLength-1; x++){
					CycleUnitDS cycleOutputX = cycleOutput[x];
					CycleUnitDS cycleOutputNext = cycleOutput[x + 1];
					if (cycleOutputNext.menstruationStartConfirm < 100) {
						daysData.addAll(cycleOutGetDayOut(cycleOutputX, 0, cycleOutputNext));
					} else {
						daysData.addAll(cycleOutGetDayOut(cycleOutputX, 1, cycleOutputNext));
					}
				}
				daysData.addAll(cycleOutGetDayOut(cycleOutput[x], 0, cycleOutput[x]));
				AppLog.i("alDayOutputList, vvvv   6.1");
			}else{
				//插入的是历史周期的经期结束
				AppLog.i("alDayOutputList, vvvv   7");
				int y = 0;
				for(y =0; y< cycleInputLength;y++){
					if(cycleInput[y].cycleNumber == cycleOutput[cycleOutputLength-1].cycleNumber) break;
				}
				if(y< cycleInputLength){
					//插入的第一个周期的经期结束
					daysData.addAll(cycleOutGetDayOut(cycleOutput[cycleOutputLength-1], 1, cycleInput[y+1]));
					
				}
				AppLog.i("alDayOutputList, vvvv   7.1");
			}
		}else if(type == 4){
			//删除经期开始
			if(cycleInput[0].cycleNumber == cycleOutput[0].cycleNumber-1){
			//删除的是历史周期的经期开始
				AppLog.i("alDayOutputList, vvvv   8");
				int index = findWhichCycleDelete(cycleInput,cycleOutput,cycleInputLength);
				if(index == 0){
				//删除的是第一个周期的经期开始
					long start = cycleInput[0].menstruationStartConfirm;
					long end = cycleOutput[0].menstruationStartConfirm;
					daysData.addAll(deleteDaysOutput(start,end,10,10));
				}else{
				//删除的是中间周期的经期开始	
					
					if(index != -1){
						CycleUnitDS cycleOutputX = cycleOutput[index-1];
						CycleUnitDS cycleOutputNext = cycleOutput[index];
						daysData.addAll(cycleOutGetDayOut(cycleOutputX, 1, cycleOutputNext));
					}
				}
				AppLog.i("alDayOutputList, vvvv   8.1");
			}else if(cycleInput[cycleInputLength-1].cycleNumber == cycleOutput[0].cycleNumber+1){
			//删除的是当前周期的经期开始
				AppLog.i("alDayOutputList, vvvv   9");
				for(x = 0; x < cycleOutputLength-1; x++){
					CycleUnitDS cycleOutputX = cycleOutput[x];
					daysData.addAll(cycleOutGetDayOut(cycleOutputX, 0, cycleOutputX));
				}
				AppLog.i("alDayOutputList, vvvv   9.1");
			}
		}else if(type == 5){
			// 删除经期结束
			AppLog.i("alDayOutputList, vvvv   10");
			if(true){
			//删除的是当前周期的经期结束
				for(x = 0; x < cycleOutputLength-1; x++){
					CycleUnitDS cycleOutputX = cycleOutput[x];
					daysData.addAll(cycleOutGetDayOut(cycleOutputX, 0, cycleOutputX));
				}
			}else{
			//删除的是历史周期的经期结束	
			}
			AppLog.i("alDayOutputList, vvvv   10.1");
		}else if(type == 7){
			AppLog.i("alDayOutputList, vvvv   11");
			for(x = 0; x < cycleOutputLength-1; x++){
				daysData.addAll(cycleOutGetDayOut(cycleInput[cycleInputLength-1],1,cycleOutput[0]));
				CycleUnitDS cycleOutputX = cycleOutput[x];
				CycleUnitDS cycleOutputNext = cycleOutput[x + 1];
				if (cycleOutputNext.menstruationStartConfirm < 100) {
					daysData.addAll(cycleOutGetDayOut(cycleOutputX, 0, cycleOutputNext));
				} else {
					daysData.addAll(cycleOutGetDayOut(cycleOutputX, 1, cycleOutputNext));
				}
			}
			AppLog.i("alDayOutputList, vvvv   11.1");
		}else if(type == 11 || type ==12 || type == 13 || type == 14){
			//
			AppLog.i("alDayOutputList, vvvv   12");
			for(x = 0; x < cycleOutputLength-1; x++){
				CycleUnitDS cycleOutputX = cycleOutput[x];
				daysData.addAll(cycleOutGetDayOut(cycleOutputX, 0, cycleOutputX));
			}
			AppLog.i("alDayOutputList, vvvv   12.1");
		}else if(type == 23 || type ==24 || type == 25){
			AppLog.i("alDayOutputList, vvvv   13");
			int y = 0;
			int nextNum = cycleOutput[0].cycleNumber+1;
			for(y = 0; y < cycleInputLength; y++){
				if(cycleInput[y].cycleNumber == nextNum) break;
			}
			if(y != cycleInputLength 
					&& cycleInput[y-1].menstruationStartConfirm == cycleOutput[0].menstruationStartConfirm){
				daysData.addAll(cycleOutGetDayOut(cycleOutput[0], 1, cycleInput[y]));
			}
			AppLog.i("alDayOutputList, vvvv   13.1");
		}
		

		for(int i = 0; i < cycleInput.length; i++)
			AppLog.i("alDayOutputList, i = " + i + ", cycleInput.cycleNumber = " + cycleInput[i].cycleNumber +
					", menstruationStartConfirm =" + DateUtil.getDateTimeStr(cycleInput[i].menstruationStartConfirm) +
					", menstruationStartForecast = " + DateUtil.getDateTimeStr(cycleInput[i].menstruationStartForecast));

		AppLog.i("alDayOutputList, cycleOutputLength = " + cycleOutputLength);
		for(int i = 0; i < cycleOutputLength; i++)
			AppLog.i("alDayOutputList, i2 = " + i + ", cycleOutput.cycleNumber = " + cycleOutput[i].cycleNumber +
					", menstruationStartConfirm =" + DateUtil.getDateTimeStr(cycleOutput[i].menstruationStartConfirm) +
					", menstruationStartForecast = " + DateUtil.getDateTimeStr(cycleOutput[i].menstruationStartForecast));

		for(int i = 0; i < daysData.size(); i++)
			AppLog.i("alDayOutputList, i = " + i + ", daysData = " + DateUtil.getDateTimeStr(daysData.get(i).date));

		return daysData;
	}
    //type :1 说明本周期的经期开始和下周起的经期开始都是确认的
	//type :0 说明本周起的经期开始是确认的，但是下周期的经期开始是预测的
	public static ArrayList<DayUnitDSOutput> cycleOutGetDayOut(CycleUnitDS cycleOutput,int type,CycleUnitDS cycleOutputNext){
		//ArrayList<DayUnitDSOutput> daysData = new ArrayList<DayUnitDSOutput>();
		boolean psConfirm = false;
		boolean peConfirm = false;
		boolean ovConfirm = false;
		boolean dgsConfirm = false;
		boolean dgeConfirm = false;
		long ps = 0;
		long pe = 0;
		long ovDay = 0;
		long nextPS = 0;
		long dgs = 0;
		long dge = 0;
		long _tDWEC = cycleOutput.dangerWindowEndConfirm;
		long _tDWSC = cycleOutput.dangerWindowStartConfirm;
		long _tPSC  = cycleOutput.menstruationStartConfirm;
		long _tPEC  = cycleOutput.menstruationEndConfirm;
		long _tOVC  = cycleOutput.ovulationDayConfirm;
		if(_tPSC > 100){
			ps = cycleOutput.menstruationStartConfirm;
			psConfirm = true;
		}else{
			ps = cycleOutput.menstruationStartForecast;
			
		}
		if(_tPEC > 100){
			pe = cycleOutput.menstruationEndConfirm;
			peConfirm = true;
		}else{
			pe = cycleOutput.menstruationEndForecast;
		}
		if(_tOVC > 100){
			ovDay = cycleOutput.ovulationDayConfirm;
			ovConfirm = true;
		}else{
			ovDay = cycleOutput.ovulationDayForecast;
		}
		if(_tDWEC > 100){
			dgeConfirm = true;
			dge = cycleOutput.dangerWindowEndConfirm;
		}else{
			dge = cycleOutput.dangerWindowEndForecast;
			
		}
		if(_tDWSC > 100){
			dgsConfirm = true;
			dgs = cycleOutput.dangerWindowStartConfirm;
		}else{
			dgs = cycleOutput.dangerWindowStartForecast;
			if(dgs < 100){
				dgs = pe + 24*3600;
				cycleOutput.dangerWindowStartForecast = dgs;
			}
			int diff = getDaysDiff(ps, pe);
			if(diff < 6){
				dgs = ps+5*24*3600;
				cycleOutput.dangerWindowStartForecast = dgs;
			}else{
				dgs = pe + 24*3600;
				cycleOutput.dangerWindowStartForecast = dgs;
			}
		}
		
		
		if(dge < 100 || dgs<100 || ps<100 || pe < 100 || ovDay < 100){
			ArrayList<DayUnitDSOutput> daysData = new ArrayList<DayUnitDSOutput>();
			return daysData;
		}
		if(type == 0) nextPS = cycleOutput.nextMenstruationStartForecast;
		if(type == 1) nextPS = cycleOutputNext.menstruationStartConfirm > 100 ?cycleOutputNext.menstruationStartConfirm:
				cycleOutputNext.menstruationStartForecast;
		return outputDaysUnit(ps, psConfirm, pe, peConfirm, ovDay, ovConfirm, nextPS, dgs, dgsConfirm, dge, dgeConfirm);
	}
    
	public static int getDaysDiff(long start,long end){
		long x = (end - start)/3600/24;
		return (int)x;
	}
    
	public static ArrayList<DayUnitDSOutput> outputDaysUnit(long tps,boolean psConfirm,long tpe,boolean peConfirm,long tov,boolean ovConfirm,long tnps,
			long tdgs,boolean dgsConfirm,long tdge,boolean dgeConfirm){
		ArrayList<DayUnitDSOutput> daysDataList = new ArrayList<DayUnitDSOutput>();
		int pFlag = (int)((tpe - tps)/(24*3600)+1);
	 	int foFlag = (int)((tov - tps)/(24*3600)-5);
		int ovFlag = (int)((tov - tps)/(24*3600)+2);
		int luFlag = (int)((tnps - tps)/(24*3600));
		
		int dgBefore = (int)((tdgs - tps)/(24*3600));
		int dgAfter = (int)((tdge - tps)/(24*3600));
		int result = 0;
		if(foFlag < pFlag){
			if(peConfirm){
				foFlag = pFlag+1;
				if(ovFlag<foFlag){
					ovFlag = foFlag;
				}
			}else{
				if(foFlag > 1){
					pFlag = foFlag - 1;
				}else{
					pFlag = 1;
					foFlag = 2;
				}
			}
			
			
		}
		if(dgBefore <= pFlag){
			dgBefore = 	pFlag+1;
		}
		if(luFlag < ovFlag){

			luFlag = ovFlag + 1; 
			result = 1;
		}
		int x = 0;
		for(x = 0; x < luFlag; x++){
			DayUnitDSOutput daysData = new DayUnitDSOutput();
			//this used to keep the pointer not out memory! 
			//if(daysData[x+dayOutputStartIndex].date != 0 && daysData[x+dayOutputStartIndex].homePageMenstruationEnd != 0 && daysData[x+dayOutputStartIndex].dayOfCycle != 0) break;
			long tx = tps + x*3600*24;

			daysData.date = tx;
			

			daysData.dayOfCycle = x+1;
			

			daysData.homePageNextMenstruation =  luFlag - x;
			

			if(x < ovFlag){
				daysData.homePageConceptionChance = (float) (0.3333 * (float)x / (ovFlag - 1));
			}else if(x == ovFlag){
				daysData.homePageConceptionChance = (float) 0.253;
			}else if(x == ovFlag + 1){
				daysData.homePageConceptionChance = (float) 0.173;
			}else if(x == ovFlag + 2){
				daysData.homePageConceptionChance = (float) 0.096;
			}else{
				daysData.homePageConceptionChance = (float) 0.006;
			}
			 
			daysData.homePageMenstruationEnd = pFlag - x - 1;

			daysData.homePageOvulation = ovFlag - x - 2;
			if(x < pFlag){

				if(psConfirm && !peConfirm){
					if(x == 0){
						daysData.periodAchieveConfirm = 1;
						daysData.periodAchieveForecast = 0;
						daysData.periodAvoidConfirm = 1;
						daysData.periodAvoidForecast = 0;
					}else{
						daysData.periodAchieveForecast = 1;
						daysData.periodAchieveConfirm = 0;
						daysData.periodAvoidConfirm = 0;
						daysData.periodAvoidForecast = 1;
					}

				}else if(psConfirm && peConfirm){
					daysData.periodAchieveConfirm = 1;
					daysData.periodAchieveForecast = 0;
					daysData.periodAvoidConfirm = 1;
					daysData.periodAvoidForecast = 0;
				}else{
					daysData.periodAchieveForecast = 1;
					daysData.periodAchieveConfirm = 0;
					daysData.periodAvoidConfirm = 0;
					daysData.periodAvoidForecast = 1;
				}
				
				
				
				
			
			}else if(x < foFlag){
				if(peConfirm && ovConfirm){
					daysData.periodAchieveForecast = 0;
					daysData.periodAchieveConfirm = 2;
				}else{
					daysData.periodAchieveForecast = 2;
					daysData.periodAchieveConfirm = 0;
				}

			}else if(x < ovFlag){
				if(ovConfirm){
					if(x == ovFlag - 2){
						daysData.periodAchieveForecast = 0;
						daysData.periodAchieveConfirm = 4;
					}else{
						daysData.periodAchieveForecast = 0;
						daysData.periodAchieveConfirm = 3;
						
					}
					
				}else{
					if(x == ovFlag - 2){
						daysData.periodAchieveForecast = 4;
						daysData.periodAchieveConfirm = 0;
					}else{
						daysData.periodAchieveForecast = 3;
						daysData.periodAchieveConfirm = 0;
					}
				}
				

			}else if(x < luFlag){
				if(ovConfirm){
					daysData.periodAchieveForecast = 0;
					daysData.periodAchieveConfirm = 5;
				}else{
					daysData.periodAchieveForecast = 5;
					daysData.periodAchieveConfirm = 0;
				}
				
			}
			if(x >= pFlag){
				if(x < dgBefore){
					if(dgsConfirm){
						daysData.periodAvoidConfirm = 2;
						daysData.periodAvoidForecast = 0;
					}else{
						daysData.periodAvoidConfirm = 0;
						daysData.periodAvoidForecast = 2;
					}
				}else if(x < dgAfter){
					if(dgeConfirm && dgsConfirm){
						daysData.periodAvoidConfirm = 3;
						daysData.periodAvoidForecast = 0;
					}else{
						daysData.periodAvoidConfirm = 0;
						daysData.periodAvoidForecast = 3;
					}
				
				}else{
					if(dgeConfirm){
						daysData.periodAvoidConfirm = 4;
						daysData.periodAvoidForecast = 0;
					}else{
						daysData.periodAvoidConfirm = 0;
						daysData.periodAvoidForecast = 4;
					}
				}
			}
			daysDataList.add(x,daysData);
		}
		return daysDataList;
	}
    
	public static ArrayList<DayUnitDSOutput>  deleteDaysOutput(long start,long end,int daysIndexStart,int deleteFirstCycleFore10daysData){
		ArrayList<DayUnitDSOutput> daysOutputList = new ArrayList<DayUnitDSOutput>();

		if(start > end || start < 1000000 || end < 1000000)
			return daysOutputList;

		int diff = getDaysDiff(start, end);
		int x = 0;
		
		for(; x < diff-deleteFirstCycleFore10daysData; x++){
			DayUnitDSOutput daysOutput = new DayUnitDSOutput();
			daysOutput.date = start+x*24*3600;
			daysOutput.periodAchieveForecast = 99;
			daysOutputList.add(x,daysOutput);
		}
		for(x = diff-deleteFirstCycleFore10daysData; x < diff ;x++ ){
			DayUnitDSOutput daysOutput = new DayUnitDSOutput();
			daysOutput.date = start+x*24*3600;
			daysOutput.homePageNextMenstruation = diff - x;
			daysOutput.homePageConceptionChance = 0;
			daysOutput.homePageOvulation=0;
			daysOutput.periodAchieveConfirm=5;
			daysOutput.periodAchieveForecast=5;
			daysOutput.periodAvoidConfirm=4;
			daysOutput.periodAvoidForecast=4;
			daysOutputList.add(x,daysOutput);
		}
		for(x = diff; x < diff + deleteFirstCycleFore10daysData;x++){
			DayUnitDSOutput daysOutput = new DayUnitDSOutput();
			daysOutput.date = start - (x+1-diff)*24*3600;
			daysOutput.periodAchieveForecast = 99;
			daysOutputList.add(x,daysOutput);
		}
		
		return daysOutputList;
	}
    
	public static ArrayList<DayUnitDSOutput> outputExtraDayBeforeAll(long nextPS,int delayCount,int startIndex){
		int xx = delayCount;
		ArrayList<DayUnitDSOutput> daysOutputList = new ArrayList<DayUnitDSOutput>();
		if(xx <= 0) return daysOutputList;

		for(;xx > 0; xx--){
			DayUnitDSOutput daysOutput = new DayUnitDSOutput();
			
			daysOutput.date = nextPS-(delayCount-xx+1)*24*3600;
			daysOutput.dayOfCycle = 0;
			daysOutput.homePageConceptionChance = 0;
			daysOutput.homePageMenstruationEnd = 0;
			daysOutput.homePageNextMenstruation = delayCount+1-xx;
			daysOutput.homePageOvulation=0;
			daysOutput.periodAchieveConfirm=5;
			daysOutput.periodAchieveForecast=5;
			daysOutput.periodAvoidConfirm=4;
			daysOutput.periodAvoidForecast=4;
			daysOutputList.add(0,daysOutput);
		}
		return daysOutputList;
	}
    
	public static int getCycleValidCount(CycleUnitDS[] cycle){
        if(cycle.length == 0) return 0;
		int x = 0;
		for(;x < 17; x++){
			if(cycle[x].menstruationStartConfirm <=0 && cycle[x].menstruationStartForecast<=0)
				break;
		}
		return x;
	}
	
	public static int findWhichCycleDelete(CycleUnitDS[] cycleInput,CycleUnitDS[] cycleOutput,int cycleInputLength){
        int x = 0;
		for(x = 0; x < cycleInputLength; x++){
			if(cycleInput[x].menstruationStartConfirm != cycleOutput[x].menstruationStartConfirm)
				break;
		}
		if(x == cycleInputLength) return -1;
		else return x;
	}
}
